﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFMINERP.COM;
using WCFMINERP.Contracts.DataContracts;
using WCFMINERP.DAL;
using WCFMINERP.InnerFactoryContracts.DataContracts;

namespace WCFMINERP.BLL.LatticePartition
{
    public class LatticePartitionBll
    {
        /// <summary>
        /// 根据工厂id获取格子分区
        /// </summary>
        /// <param name="oupshopid"></param>
        /// <returns></returns>
        public List<Contracts.DataContracts.LatticePartition> GetLatticePartitionList(int oupshopid)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {

                List<Contracts.DataContracts.LatticePartition> ModelList =
                    new List<Contracts.DataContracts.LatticePartition>();
                List<T_LatticePartition> Models =
                    db.T_LatticePartition.Where(
                        p => p.OutShopId == oupshopid && p.State != (int) LatticePartitionState.Alreadydeleted).ToList();

                List<T_LatticeRecord> listRecord = db.T_LatticeRecord.ToList();

                foreach (T_LatticePartition part in Models)
                {
                    Contracts.DataContracts.LatticePartition Mode = new Contracts.DataContracts.LatticePartition();
                    Mode.Pid = part.Pid;
                    Mode.outShopId = part.OutShopId;
                    Mode.LatticeHeadName = part.LatticeHeadName;
                    Mode.RowsNum = part.RowsNum;
                    Mode.ColumnNum = part.ColumnNum;
                    Mode.State = part.State;


                    if (!part.T_Lattice.IsLoaded)
                    {
                        part.T_Lattice.Load();
                    }
                    foreach (T_Lattice itm in part.T_Lattice)
                    {
                        if (Mode.listLattice == null)
                        {
                            Mode.listLattice = new List<Lattice>();
                        }
                        Lattice ice = new Lattice();
                        ice.LatticeID = itm.LatticeID;
                        ice.LatticeName = itm.LatticeName;
                        ice.OrderID = itm.OrderID;
                        ice.Pid = itm.Pid;
                        ice.ScanningTime = itm.ScanningTime;
                        ice.State = itm.State;
                        if (ice.latticeRecordList == null)
                            ice.latticeRecordList = new List<LatticeRecord>();
                        List<T_LatticeRecord> list = listRecord.Where(p => p.LatticeID == ice.LatticeID).ToList();
                        foreach (T_LatticeRecord rec in list)
                        {
                            LatticeRecord recmodel = new LatticeRecord();
                            recmodel.ID = rec.ID;
                            recmodel.LatticeID = rec.LatticeID;
                            recmodel.OrderInfoID = rec.OrderInfoID;
                            if (rec.CreateTime != null)
                            {
                                recmodel.CreateTime = Convert.ToDateTime(rec.CreateTime);
                            }
                            ice.latticeRecordList.Add(recmodel);
                        }
                        if (ice.latticeRecordList != null && ice.latticeRecordList.Count > 0)
                        {
                            Tb_OrderBag orderbaginfo = db.Tb_OrderBag.FirstOrDefault(p => p.OrderBagId == itm.OrderID);
                            ice.OrderName = orderbaginfo.BagName;
                            ice.OrderCode = orderbaginfo.BagCode;

                            OrderBagInfo baginfo = new OrderBagInfo();
                            baginfo.OrderbagID = orderbaginfo.OrderBagId;
                            baginfo.OrderbagName = orderbaginfo.BagName;
                            if (!orderbaginfo.ShopReference.IsLoaded)
                            {
                                orderbaginfo.ShopReference.Load();
                            }
                            Contracts.DataContracts.ShopInfo shopmodel = new Contracts.DataContracts.ShopInfo();
                            shopmodel.Tel1 = orderbaginfo.Shop.Tel1;
                            shopmodel.ShopId = orderbaginfo.Shop.ShopId;
                            shopmodel.shopName = orderbaginfo.Shop.shopName;
                            shopmodel.shopKeeper = orderbaginfo.Shop.shopKeeper;
                            baginfo.shopinfo = shopmodel;

                            baginfo.ShopName = orderbaginfo.Shop.shopName;
                            baginfo.ShopId = orderbaginfo.Shop.ShopId;
                            baginfo.OrderTime = orderbaginfo.OrderTime;
                            baginfo.FetchTime = orderbaginfo.FetchTime;
                            baginfo.Status = orderbaginfo.Status;
                            baginfo.Priority = orderbaginfo.Priority;
                            baginfo.Remark = orderbaginfo.Remark;

                            if (!orderbaginfo.orderInfo.IsLoaded)
                            {
                                orderbaginfo.orderInfo.Load();
                            }
                            baginfo.ctmNames = "";
                            foreach (orderInfo item in orderbaginfo.orderInfo)
                            {
                                if (!item.WorkOrderFlowList.IsLoaded)
                                {
                                    item.WorkOrderFlowList.Load();
                                }
                                if (!baginfo.ctmNames.Contains(item.CtmName))
                                {
                                    baginfo.ctmNames += item.CtmName + ",";
                                }
                            }
                            if (!string.IsNullOrEmpty(baginfo.ctmNames))
                            {
                                baginfo.ctmNames = baginfo.ctmNames.TrimEnd(',');
                            }
                            ice.OrderInfoCount = orderbaginfo.orderInfo.Count;
                            ice.OrderbagModel = baginfo;
                        }
                        Mode.listLattice.Add(ice);
                    }
                    ModelList.Add(Mode);
                }
                return ModelList;
            }
        }


        public Contracts.DataContracts.LatticePartition GetLatticePartitionModel(int oupshopid, int pid)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                T_LatticePartition part =
                    db.T_LatticePartition.FirstOrDefault(
                        p =>
                            p.OutShopId == oupshopid && p.Pid == pid &&
                            p.State != (int) LatticePartitionState.Alreadydeleted);
                Contracts.DataContracts.LatticePartition Mode = new Contracts.DataContracts.LatticePartition();

                List<T_LatticeRecord> listRecord = db.T_LatticeRecord.ToList();

                Mode.Pid = part.Pid;
                Mode.outShopId = part.OutShopId;
                Mode.LatticeHeadName = part.LatticeHeadName;
                Mode.RowsNum = part.RowsNum;
                Mode.ColumnNum = part.ColumnNum;
                Mode.State = part.State;

                if (!part.T_Lattice.IsLoaded)
                {
                    part.T_Lattice.Load();
                }
                foreach (T_Lattice itm in part.T_Lattice)
                {
                    if (Mode.listLattice == null)
                    {
                        Mode.listLattice = new List<Lattice>();
                    }
                    Lattice ice = new Lattice();
                    ice.LatticeID = itm.LatticeID;
                    ice.LatticeName = itm.LatticeName;
                    ice.OrderID = itm.OrderID;
                    ice.Pid = itm.Pid;
                    ice.ScanningTime = itm.ScanningTime;
                    ice.State = itm.State;

                    if (ice.latticeRecordList == null)
                        ice.latticeRecordList = new List<LatticeRecord>();
                    List<T_LatticeRecord> list = listRecord.Where(p => p.LatticeID == ice.LatticeID).ToList();
                    foreach (T_LatticeRecord rec in list)
                    {
                        LatticeRecord recmodel = new LatticeRecord();
                        recmodel.ID = rec.ID;
                        recmodel.LatticeID = rec.LatticeID;
                        recmodel.OrderInfoID = rec.OrderInfoID;
                        if (rec.CreateTime != null)
                        {
                            recmodel.CreateTime = Convert.ToDateTime(rec.CreateTime);
                        }
                        ice.latticeRecordList.Add(recmodel);
                    }
                    if (ice.latticeRecordList != null && ice.latticeRecordList.Count > 0)
                    {
                        Tb_OrderBag orderbaginfo = db.Tb_OrderBag.FirstOrDefault(p => p.OrderBagId == itm.OrderID);

                        ice.OrderName = orderbaginfo.BagName;
                        ice.OrderCode = orderbaginfo.BagCode;

                        OrderBagInfo baginfo = new OrderBagInfo();
                        baginfo.OrderbagID = orderbaginfo.OrderBagId;
                        baginfo.OrderbagName = orderbaginfo.BagName;
                        if (!orderbaginfo.ShopReference.IsLoaded)
                        {
                            orderbaginfo.ShopReference.Load();
                        }
                        Contracts.DataContracts.ShopInfo shopmodel = new Contracts.DataContracts.ShopInfo();
                        shopmodel.Tel1 = orderbaginfo.Shop.Tel1;
                        shopmodel.ShopId = orderbaginfo.Shop.ShopId;
                        shopmodel.shopName = orderbaginfo.Shop.shopName;
                        shopmodel.shopKeeper = orderbaginfo.Shop.shopKeeper;
                        baginfo.shopinfo = shopmodel;
                        baginfo.ShopName = orderbaginfo.Shop.shopName;
                        baginfo.ShopId = orderbaginfo.Shop.ShopId;
                        baginfo.OrderTime = orderbaginfo.OrderTime;
                        baginfo.FetchTime = orderbaginfo.FetchTime;
                        baginfo.Status = orderbaginfo.Status;
                        baginfo.Priority = orderbaginfo.Priority;
                        baginfo.Remark = orderbaginfo.Remark;

                        if (!orderbaginfo.orderInfo.IsLoaded)
                        {
                            orderbaginfo.orderInfo.Load();
                        }
                        baginfo.ctmNames = "";
                        foreach (orderInfo item in orderbaginfo.orderInfo)
                        {
                            if (!item.WorkOrderFlowList.IsLoaded)
                            {
                                item.WorkOrderFlowList.Load();
                            }
                            if (!baginfo.ctmNames.Contains(item.CtmName))
                            {
                                baginfo.ctmNames += item.CtmName + ",";
                            }
                        }
                        if (!string.IsNullOrEmpty(baginfo.ctmNames))
                        {
                            baginfo.ctmNames = baginfo.ctmNames.TrimEnd(',');
                        }
                        ice.OrderInfoCount = orderbaginfo.orderInfo.Count(s => s.status == 1);
                        ice.OrderbagModel = baginfo;
                    }
                    Mode.listLattice.Add(ice);
                }
                return Mode;
            }
        }

        /// <summary>
        /// 添加一条格子分区记录
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public int AddLatticePartition(Contracts.DataContracts.LatticePartition models)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                T_LatticePartition model = new T_LatticePartition();
                model.ColumnNum = models.ColumnNum;
                model.LatticeHeadName = models.LatticeHeadName;
                model.OutShopId = models.outShopId;
                model.RowsNum = models.RowsNum;
                model.State = (int) LatticePartitionState.Aormal;
                db.T_LatticePartition.AddObject(model);

                db.SaveChanges();

                int count = 0;
                for (int i = 0; i < models.RowsNum; i++)
                {
                    for (int j = 0; j < models.ColumnNum; j++)
                    {
                        T_Lattice latt = new T_Lattice();
                        latt.Pid = model.Pid;
                        count++;
                        latt.LatticeName = models.LatticeHeadName + count;
                        latt.OrderID = 0;
                        latt.ScanningTime = DateTime.Now;
                        latt.State = (int) LatticeState.Aormal;
                        db.T_Lattice.AddObject(latt);
                        db.SaveChanges();
                    }
                }
                return model.Pid;
            }
        }

        /// <summary>
        /// 删除一条格子分区记录
        /// </summary>
        /// <param name="Pid"></param>
        /// <returns></returns>
        public int DeleteLatticePartition(int Pid)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                T_LatticePartition model = db.T_LatticePartition.FirstOrDefault(p => p.Pid == Pid);
                model.State = (int) LatticePartitionState.Alreadydeleted; //将状态改为已经删除           
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 修改一条格子分区记录
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public int UpdateLatticePartition(Contracts.DataContracts.LatticePartition models)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                List<T_Lattice> listLatt = db.T_Lattice.Where(p => p.Pid == models.Pid).ToList();
                foreach (T_Lattice item in listLatt)
                {
                    db.T_Lattice.DeleteObject(item);
                }

                db.SaveChanges(); //删除原来的

                T_LatticePartition model = db.T_LatticePartition.FirstOrDefault(p => p.Pid == models.Pid);

                model.RowsNum = models.RowsNum;
                model.ColumnNum = models.ColumnNum;
                model.LatticeHeadName = models.LatticeHeadName;
                int id = db.SaveChanges(); //修改


                int count = 0;
                for (int i = 0; i < models.RowsNum; i++)
                {
                    for (int j = 0; j < models.ColumnNum; j++)
                    {
                        T_Lattice latt = new T_Lattice();
                        latt.Pid = model.Pid;
                        count++;
                        latt.LatticeName = models.LatticeHeadName + count;
                        latt.OrderID = 0;
                        latt.ScanningTime = DateTime.Now;
                        latt.State = (int) LatticeState.Aormal;
                        db.T_Lattice.AddObject(latt);
                        db.SaveChanges(); //添加
                    }
                }

                return id;
            }
        }

        /// <summary>
        /// 根据工厂id,修改当前正在使用的格子
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public int UpdateLatticePartitionState(Contracts.DataContracts.LatticePartition models)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                T_LatticePartition model = db.T_LatticePartition.FirstOrDefault(p => p.Pid == models.Pid);
                model.State = models.State;
                return db.SaveChanges();
            }
        }




        /// <summary>
        /// 扫描产品code,成功返回订单信息
        /// </summary>
        /// <param name="OperateUid"></param>
        /// <param name="techId">当等于-1时不需要检验流程是不是在当前流程</param>
        /// <param name="code"></param>
        /// <param name="pid"></param>
        /// <param name="latticemodel"></param>
        /// <param name="mess"></param>
        /// <returns></returns>
        public int ScanningOrderInfoCode(int OperateUid, int techId, string code, int pid, out Lattice latticemodel,
            out string mess)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                bool b = true;
                int ret = -1;
                mess = "";
                latticemodel = new Lattice();
                orderInfo info = db.orderInfo.FirstOrDefault(p => p.OrderProductCode == code);
                if (info == null)
                {
                    mess = "产品号不存在或产品全部流程已完成！";
                    ret = 1;
                    return ret;
                }



                #region   取得订单信息

                //if (!info.Tb_OrderBagReference.IsLoaded)
                //{
                //    info.Tb_OrderBagReference.Load();
                //}
                Tb_OrderBag orderbaginfo = info.Tb_OrderBag;

                latticemodel.OrderName = orderbaginfo.BagName;
                latticemodel.OrderCode = orderbaginfo.BagCode;
                latticemodel.OrderID = orderbaginfo.OrderBagId;

                //if (!orderbaginfo.orderInfo.IsLoaded)
                //{
                //    orderbaginfo.orderInfo.Load();
                //}
                latticemodel.OrderInfoCount = orderbaginfo.orderInfo.Count;

                #endregion

                //if (!info.Tb_OrderBagReference.IsLoaded)
                //{
                //    info.Tb_OrderBagReference.Load();
                //}
                //if (!info.Tb_OrderBag.ShopReference.IsLoaded)
                //{
                //    info.Tb_OrderBag.ShopReference.Load();
                //}

                if (techId != -1) //当岗位!=-1需要检验是不是在当前流程
                {
                    b = false;
                    users OperateUser = db.users.FirstOrDefault(p => p.userID == OperateUid);
                    if (info.status != (int) OrderInfoStatus.Doing)
                    {
                        switch ((OrderInfoStatus) info.status)
                        {

                            case OrderInfoStatus.Error:
                                mess = "该产品有错误,具体情况请查询客服！";
                                ret = 2;
                                return ret;
                            case OrderInfoStatus.Stop:
                                mess = "该产品是已停止,具体情况请查询客服！";
                                ret = 3;
                                return ret;
                            case OrderInfoStatus.Completed:
                                mess = "产品是已完成,具体情况请查询客服！";
                                ret = 4;
                                return ret;
                            default:
                                mess = "该产品无法处理,具体情况请查询客服！";
                                ret = 5;
                                return ret;
                        }

                    }
                    else
                    {
                        if (!info.WorkOrderFlowList.IsLoaded)
                        {
                            info.WorkOrderFlowList.Load();
                        }
                        int printBillType = (int) TechType.PrintBill;
                        WorkOrderFlowList wfl =
                            info.WorkOrderFlowList.FirstOrDefault(
                                w =>
                                    w.Status == (int) OrderFlowListStatus.Useable &&
                                    w.tb_Tech.TechType == printBillType);
                        if (wfl == null)
                        {
                            mess = "未到本岗";
                            ret = 6;
                            return ret;
                        }
                        else
                        {
                            if (!wfl.tb_TechReference.IsLoaded)
                            {
                                wfl.tb_TechReference.Load();
                            }
                            if (wfl.tb_Tech.TechId == techId) //当前流程在分堆
                            {
                                //调用商户分堆模块
                                b = true;
                            }
                            else //当前流程不在理件分堆
                            {
                                ret = 7;
                                mess = "当前流程不在理件分堆";
                            }
                        }
                    }

                }
                if (b)
                {
                    #region

                    //当前分区
                    T_LatticePartition partitioninfo = db.T_LatticePartition.FirstOrDefault(p => p.Pid == pid);


                    //获取当前分区下面的所有格子
                    List<T_Lattice> latticeinfoList = db.T_Lattice.Where(p => p.Pid == partitioninfo.Pid).ToList();

                    //获取不是当前分区下面的所有格子
                    List<T_Lattice> NOlatticeinfoList =
                        db.T_Lattice.Where(p => p.Pid != partitioninfo.Pid && p.State > 0).ToList();

                    //判断在其它分区有没有存在此订单的格子
                    if (NOlatticeinfoList.Any(p => p.OrderID == orderbaginfo.OrderBagId))
                    {
                        T_Lattice ice = NOlatticeinfoList.FirstOrDefault(p => p.OrderID == orderbaginfo.OrderBagId);
                        if (!ice.T_LatticePartitionReference.IsLoaded)
                        {
                            ice.T_LatticePartitionReference.Load();
                        }
                        mess = "分区“" + ice.T_LatticePartition.LatticeHeadName + "”已经入格";
                        return 8;
                    }

                    //判断所有格子中是否已经扫描过此订单
                    if (latticeinfoList.Any(p => p.OrderID == orderbaginfo.OrderBagId)) //存在，则添加一条扫描产品记录
                    {
                        T_Lattice lattinfo = latticeinfoList.FirstOrDefault(p => p.OrderID == orderbaginfo.OrderBagId);

                        if (
                            db.T_LatticeRecord.Any(
                                p => p.LatticeID == lattinfo.LatticeID && p.OrderInfoID == info.OrderProductId))
                        {
                            mess = "重复," + lattinfo.LatticeName;
                            return 9;
                        }

                        T_LatticeRecord recordmodel = new T_LatticeRecord();
                        recordmodel.LatticeID = lattinfo.LatticeID;
                        recordmodel.OrderInfoID = info.OrderProductId;
                        recordmodel.CreateTime = DateTime.Now;
                        db.T_LatticeRecord.AddObject(recordmodel); //给扫描格子记录添加数据

                        db.SaveChanges(); //保存更改
                        if (lattinfo.LatticeName.Contains(lattinfo.T_LatticePartition.LatticeHeadName))
                        {
                            mess = lattinfo.LatticeName.Replace(lattinfo.T_LatticePartition.LatticeHeadName, "");
                        }
                        else
                        {
                            mess = lattinfo.LatticeName;
                        }
                        ret = 0;
                        latticemodel.LatticeID = lattinfo.LatticeID;
                        return 0;
                    }
                    else //不存在，则按顺序分配一个格子给新的订单
                    {
                        if (!latticeinfoList.Any(p => p.OrderID == 0))
                        {
                            mess = "格子不足";
                            return 10; //未找到改产品
                        }

                        //格子的分配规则根据LatticeID由小到大进行分配
                        T_Lattice waitLatticeinfo =
                                latticeinfoList.Where(p => p.OrderID == 0).OrderBy(p => p.LatticeID).FirstOrDefault();
                            //没有分配的格子集合,由ID由小到大取格子

                        db.T_Lattice.FirstOrDefault(p => p.LatticeID == waitLatticeinfo.LatticeID).OrderID =
                            orderbaginfo.OrderBagId; //将格子里面的订单属性给值

                        T_LatticeRecord recordmodel = new T_LatticeRecord();
                        recordmodel.LatticeID = waitLatticeinfo.LatticeID;
                        recordmodel.OrderInfoID = info.OrderProductId;
                        recordmodel.CreateTime = DateTime.Now;
                        db.T_LatticeRecord.AddObject(recordmodel); //给扫描格子记录添加数据

                        partitioninfo.State = (int) LatticePartitionState.Use; //将分区 改为使用中

                        waitLatticeinfo.State = (int) LatticeState.PretendPart; //格子已经部分装

                        db.SaveChanges(); //保存更改                        
                        mess = waitLatticeinfo.LatticeName;
                        latticemodel.LatticeID = waitLatticeinfo.LatticeID;
                        ret = 0;
                        return 0;
                    }

                    #endregion
                }

                return ret;

            }

        }


        /// <summary>
        /// 清空格子
        /// </summary>
        /// <param name="LatticeID"></param>
        /// <param name="mess"></param>
        /// <returns></returns>
        public int emptyLattice(int LatticeID, out string mess)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int retvalue = 0;
                mess = "";
                T_Lattice lattinfo = db.T_Lattice.FirstOrDefault(p => p.LatticeID == LatticeID);

                if (lattinfo == null)
                {
                    mess = "没有找到该格子";
                    return 0;
                }

                //不绑定订单
                db.T_Lattice.FirstOrDefault(p => p.LatticeID == lattinfo.LatticeID).OrderID = 0; //将格子里面的订单属性给值为0
                db.T_Lattice.FirstOrDefault(p => p.LatticeID == lattinfo.LatticeID).State = (int) LatticeState.Aormal;
                    //将格子里面的订单属性给值为0

                //清空格子记录表
                List<T_LatticeRecord> list = db.T_LatticeRecord.Where(p => p.LatticeID == lattinfo.LatticeID).ToList();
                foreach (T_LatticeRecord item in list)
                {
                    db.T_LatticeRecord.DeleteObject(item);
                }
                db.SaveChanges();



                T_LatticePartition part = new T_LatticePartition();
                if (!lattinfo.T_LatticePartitionReference.IsLoaded)
                {
                    lattinfo.T_LatticePartitionReference.Load();
                }
                part = lattinfo.T_LatticePartition;
                List<T_Lattice> listlattice = new List<T_Lattice>();
                if (!part.T_Lattice.IsLoaded)
                {
                    part.T_Lattice.Load();
                }
                listlattice = part.T_Lattice.ToList();

                bool bol = true;
                foreach (T_Lattice item in listlattice)
                {
                    if (item.OrderID > 0)
                    {
                        bol = false;
                        break;
                    }
                }
                if (bol)
                {
                    lattinfo.T_LatticePartition.State = (int) LatticePartitionState.Aormal; //未使用
                }
                else
                {
                    lattinfo.T_LatticePartition.State = (int) LatticePartitionState.Use; //使用中
                }

                db.SaveChanges();
                mess = "成功";
                return 1;
            }
        }



        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="orderid">订单ID</param>
        /// <param name="EorrMess">信息</param>
        /// <returns></returns>
        public OrderBagInfo GetOrderBag(int orderid, out string EorrMess)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                EorrMess = "";
                Tb_OrderBag baginfo = db.Tb_OrderBag.FirstOrDefault(p => p.OrderBagId == orderid);
                if (!baginfo.orderInfo.IsLoaded)
                {
                    baginfo.orderInfo.Load();
                }
                List<orderInfo> listorderinfo = baginfo.orderInfo.ToList();

                OrderBagInfo orderbaginfomodel = new OrderBagInfo();

                orderbaginfomodel.OrderbagID = baginfo.OrderBagId;
                orderbaginfomodel.OrderbagName = baginfo.BagName;
                if (!baginfo.ShopReference.IsLoaded)
                {
                    baginfo.ShopReference.Load();
                }
                Contracts.DataContracts.ShopInfo shopmodel = new Contracts.DataContracts.ShopInfo();
                shopmodel.Tel1 = baginfo.Shop.Tel1;
                shopmodel.ShopId = baginfo.Shop.ShopId;
                shopmodel.shopName = baginfo.Shop.shopName;
                shopmodel.shopKeeper = baginfo.Shop.shopKeeper;
                orderbaginfomodel.shopinfo = shopmodel;
                orderbaginfomodel.ShopId = baginfo.Shop.ShopId;
                orderbaginfomodel.OrderTime = baginfo.OrderTime;
                orderbaginfomodel.FetchTime = baginfo.FetchTime;
                orderbaginfomodel.Status = baginfo.Status;
                orderbaginfomodel.Priority = baginfo.Priority;
                orderbaginfomodel.Remark = baginfo.Remark;


                orderbaginfomodel.ctmNames = "";
                foreach (orderInfo orderModels in listorderinfo)
                {
                    OrderInfo info = new OrderInfo();
                    if (!orderModels.WorkOrderFlowList.IsLoaded)
                    {
                        orderModels.WorkOrderFlowList.Load();
                    }
                    var tch = orderModels.WorkOrderFlowList.FirstOrDefault(p => p.Status == 1); //当前在哪个流程
                    if (tch != null)
                    {
                        if (!tch.tb_TechReference.IsLoaded)
                        {
                            tch.tb_TechReference.Load();
                        }
                        info.CurrentTech = tch.tb_Tech.TechName;
                    }

                    if (!orderbaginfomodel.ctmNames.Contains(orderModels.CtmName))
                    {
                        orderbaginfomodel.ctmNames += orderModels.CtmName + ",";
                    }



                    int Status = (int) OutProcessRecordStatus.Fetched;
                    int Status2 = (int) OutProcessRecordStatus2.Complete;
                    int OrderStatus = (int) OrderInfoStatus.Doing;

                    List<OutProcessRecord> outProcessRecords =
                        db.OutProcessRecord.Where(
                                p => p.orderInfo.status == OrderStatus && p.Status != Status && p.Status2 != (int) Status2)
                            .ToList();

                    if (!orderModels.Tb_ProductReference.IsLoaded)
                    {
                        orderModels.Tb_ProductReference.Load();
                    }
                    if (!orderModels.Tb_OrderBagReference.IsLoaded)
                    {
                        orderModels.Tb_OrderBagReference.Load();
                    }

                    info.photoCount =
                        db.photoInfo.Where(p => p.orderInfo.OrderProductId == orderModels.OrderProductId).Count();

                    info.CompleteTime = orderModels.CompleteTime;
                    info.Area = orderModels.Area;
                    info.ComputerName = orderModels.ComputerName;
                    info.CtmName = orderModels.CtmName;
                    info.Height = Convert.ToSingle(orderModels.Height);
                    info.isCount = Convert.ToInt32(orderModels.isCount);
                    info.IsEdit = orderModels.IsEdit;
                    info.MultiPageNum = orderModels.MultiPageNum;

                    info.OrderBagId = orderModels.Tb_OrderBag.OrderBagId;
                    info.OrderPath = orderModels.OrderPath;
                    info.OrderProductCode = orderModels.OrderProductCode;
                    info.OrderProductId = orderModels.OrderProductId;
                    info.OrderProductName = orderModels.OrderProductName;
                    info.OrderType = orderModels.OrderType;
                    info.OrilNum = orderModels.OrilNum;
                    info.OrilOrderPath = orderModels.OrilOrderPath;
                    info.PageNum = orderModels.PageNum;
                    info.ParentProductId = orderModels.ParentProductId;
                    info.PrePriceCount = Convert.ToDecimal(orderModels.PrePriceCount);
                    info.Priority = Convert.ToInt32(orderModels.Priority);

                    info.ProductId = orderModels.Tb_Product.ProuctId;
                    info.productNum = orderModels.productNum;
                    info.ProductType = orderModels.ProductType;
                    info.Remark = orderModels.Remark;
                    info.SinglePageNum = orderModels.SinglePageNum;
                    info.Status = orderModels.status;
                    info.Width = Convert.ToSingle(orderModels.Width);

                    if (info.prodinfo == null)
                    {
                        info.prodinfo = new ProductInfo();
                    }
                    info.prodinfo.AppendPrice = orderModels.Tb_Product.AppendPrice;
                    info.prodinfo.AreaPrice = orderModels.Tb_Product.AreaPrice;
                    info.prodinfo.Height = orderModels.Tb_Product.Height;
                    info.prodinfo.PageNum = orderModels.Tb_Product.PageNum;
                    info.prodinfo.ProductCode = orderModels.Tb_Product.ProductCode;
                    info.prodinfo.ProductDes = orderModels.Tb_Product.ProductDes;
                    info.prodinfo.ProductName = orderModels.Tb_Product.ProductName;
                    info.prodinfo.ProuctId = orderModels.Tb_Product.ProuctId;
                    info.prodinfo.Status = orderModels.Tb_Product.Status;
                    info.prodinfo.UnitPrice = orderModels.Tb_Product.UnitPrice;
                    info.prodinfo.Width = orderModels.Tb_Product.Width;

                    info.prodinfo.MnemonicCode = orderModels.Tb_Product.mnemonicCode;

                    if (info.orderBagInfo == null)
                        info.orderBagInfo = new OrderBagInfo();
                    info.orderBagInfo.OrderbagName = orderModels.Tb_OrderBag.BagName;
                    if (!orderModels.Tb_OrderBag.ShopReference.IsLoaded)
                    {
                        orderModels.Tb_OrderBag.ShopReference.Load();
                    }
                    info.orderBagInfo.ShopName = orderModels.Tb_OrderBag.Shop.shopName;
                    info.orderBagInfo.ShopId = orderModels.Tb_OrderBag.Shop.ShopId;
                    info.FacthTime = orderModels.Tb_OrderBag.FetchTime;

                    Tb_Product productType = orderModels.Tb_Product;

                    productType.Tb_OutShop_ProductCategory.Load();

                    if (outProcessRecords.Count > 0)
                    {
                        switch (outProcessRecords[0].Status)
                        {
                            case (int) OutProcessRecordStatus.Outing:
                            {
                                switch (outProcessRecords[0].Status2)
                                {
                                    case (int) OutProcessRecordStatus2.Doing:
                                        info.strStatus = "正在生产";
                                        break;
                                    case (int) OutProcessRecordStatus2.Error:
                                        info.strStatus = "暂停生产";
                                        break;
                                    case (int) OutProcessRecordStatus2.Complete:
                                        info.strStatus = "生产完成";
                                        break;
                                    default:
                                        info.strStatus = "等待接收";
                                        break;
                                }
                            }
                                break;
                            case (int) OutProcessRecordStatus.InGrid:
                            {
                                info.strStatus = "未打印(在格子中)";
                            }
                                break;
                            case (int) OutProcessRecordStatus.OutWaitFenDui:
                            {
                                info.strStatus = "未打印(等待分堆)";
                            }
                                break;
                        }
                    }
                    if (orderbaginfomodel.orderinfoList == null)
                        orderbaginfomodel.orderinfoList = new List<OrderInfo>();
                    orderbaginfomodel.orderinfoList.Add(info);

                }
                if (!string.IsNullOrEmpty(orderbaginfomodel.ctmNames))
                {
                    orderbaginfomodel.ctmNames = orderbaginfomodel.ctmNames.TrimEnd(',');
                }
                return orderbaginfomodel;
            }
        }

        public Lattice GetGridInfo(int ingridid)
        {
            Lattice iceRtn = new Lattice();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                T_Lattice itm = db.T_Lattice.FirstOrDefault(p => p.LatticeID == ingridid);

                List<T_LatticeRecord> listRecord = db.T_LatticeRecord.Where(p => p.LatticeID == ingridid).ToList();

                iceRtn.LatticeID = itm.LatticeID;
                iceRtn.LatticeName = itm.LatticeName;
                iceRtn.OrderID = itm.OrderID;
                iceRtn.Pid = itm.Pid;
                iceRtn.ScanningTime = itm.ScanningTime;
                iceRtn.State = itm.State;

                if (iceRtn.latticeRecordList == null)
                    iceRtn.latticeRecordList = new List<LatticeRecord>();
                // List<T_LatticeRecord> list = listRecord;
                foreach (T_LatticeRecord rec in listRecord)
                {
                    LatticeRecord recmodel = new LatticeRecord();
                    recmodel.ID = rec.ID;
                    recmodel.LatticeID = rec.LatticeID;
                    recmodel.OrderInfoID = rec.OrderInfoID;
                    if (rec.CreateTime != null)
                    {
                        recmodel.CreateTime = Convert.ToDateTime(rec.CreateTime);
                    }
                    iceRtn.latticeRecordList.Add(recmodel);
                }
                if (iceRtn.latticeRecordList != null && iceRtn.latticeRecordList.Count > 0)
                {
                    Tb_OrderBag orderbaginfo = db.Tb_OrderBag.FirstOrDefault(p => p.OrderBagId == itm.OrderID);

                    iceRtn.OrderName = orderbaginfo.BagName;
                    iceRtn.OrderCode = orderbaginfo.BagCode;

                    OrderBagInfo baginfo = new OrderBagInfo();
                    baginfo.OrderbagID = orderbaginfo.OrderBagId;
                    baginfo.OrderbagName = orderbaginfo.BagName;
                    if (!orderbaginfo.ShopReference.IsLoaded)
                    {
                        orderbaginfo.ShopReference.Load();
                    }
                    Contracts.DataContracts.ShopInfo shopmodel = new Contracts.DataContracts.ShopInfo();
                    shopmodel.Tel1 = orderbaginfo.Shop.Tel1;
                    shopmodel.ShopId = orderbaginfo.Shop.ShopId;
                    shopmodel.shopName = orderbaginfo.Shop.shopName;
                    shopmodel.shopKeeper = orderbaginfo.Shop.shopKeeper;
                    baginfo.shopinfo = shopmodel;
                    baginfo.ShopName = orderbaginfo.Shop.shopName;
                    baginfo.ShopId = orderbaginfo.Shop.ShopId;
                    baginfo.OrderTime = orderbaginfo.OrderTime;
                    baginfo.FetchTime = orderbaginfo.FetchTime;
                    baginfo.Status = orderbaginfo.Status;
                    baginfo.Priority = orderbaginfo.Priority;
                    baginfo.Remark = orderbaginfo.Remark;

                    if (!orderbaginfo.orderInfo.IsLoaded)
                    {
                        orderbaginfo.orderInfo.Load();
                    }
                    baginfo.ctmNames = "";
                    foreach (orderInfo item in orderbaginfo.orderInfo)
                    {
                        if (!item.WorkOrderFlowList.IsLoaded)
                        {
                            item.WorkOrderFlowList.Load();
                        }
                        if (!baginfo.ctmNames.Contains(item.CtmName))
                        {
                            baginfo.ctmNames += item.CtmName + ",";
                        }
                    }
                    if (!string.IsNullOrEmpty(baginfo.ctmNames))
                    {
                        baginfo.ctmNames = baginfo.ctmNames.TrimEnd(',');
                    }
                    iceRtn.OrderInfoCount = orderbaginfo.orderInfo.Count(s => s.status == 1);
                    iceRtn.OrderbagModel = baginfo;
                }
            }
            return iceRtn;
        }
    }
}
